<?php

namespace App\Http\Utils\Currency;

/**
 * Class Order
 * @package App\Http\Utils\Currency
 */
class Order
{
    /**
     * @var string
     */
    public $userId;

    /**
     * @var string
     */
    public $shopId;

    /**
     * @var string
     */
    public $payment;

    /**
     * @var int
     */
    public $amount;

    /**
     * @var string
     */
    public $orderNum;

    /**
     * @var string
     */
    public $callback;

    /**
     * @var array
     */
    public $other = [];

    /**
     * Order constructor.
     * @param string $userId
     * @param string $shopId
     * @param string $orderNum
     * @param int $amount
     * @param string $payment
     * @param string $callback
     * @param array $other
     */
    public function __construct($userId, $shopId, $orderNum, $amount, $payment, $callback, array $other = [])
    {
        $this->userId = $userId;
        $this->shopId = $shopId;
        $this->orderNum = $orderNum;
        $this->amount = $amount;
        $this->payment = $payment;
        $this->callback = $callback;
        $this->other = $other;
    }

    /**
     * @return mixed
     * @throws \Exception
     */
    public function pay()
    {
        $payment = new $this->payment($this);
        if ($payment instanceof ThirdPay) {
            return $payment->pay();
        }
        throw new \Exception('error');
    }


    /**
     * @return string
     */
    public function body()
    {
        return isset($this->other['body']) ? $this->other['body'] : 'pay';
    }

    /**
     * @return string
     */
    public function title()
    {
        return isset($this->other['title']) ? $this->other['title'] : '商城支付';
    }

    /**
     * @return string
     */
    public function ip()
    {
        return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
    }

    /**
     * @return int
     */
    public function timeout()
    {
        return time() + 600;
    }
}